package com.jiangxjsy.crm.property;

import com.jiangxjsy.core.jwt.RsaHelper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.security.PublicKey;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/14 12:02
 */

@Data
@Component
@RefreshScope
@ConfigurationProperties(prefix = "wukong-crm.jwt")
@Slf4j
public class JwtProperty {
    /**
     * 公钥路径
     */
    private String publicKeyPath;

    /**
     * 公钥
     */
    private PublicKey publicKey;

    /**
     * Bean 被初始化时执行，加载公钥，加载失败抛出异常，结束启动
     */
    @PostConstruct
    public void init() {
        try {
            publicKey = RsaHelper.getPublicKey(publicKeyPath);
            log.debug("RSA keys load success, public key path: " + publicKeyPath);
        } catch (Exception e) {
            throw new IllegalStateException("RSA keys load failed, application cannot start.", e);
        }
    }
}
